import OverscrollPlugin from "smooth-scrollbar/plugins/overscroll";
import Scrollbar, { ScrollbarPlugin } from "smooth-scrollbar";
import type { Data2d } from "smooth-scrollbar/interfaces";

/**
 * 按下Shift键时横向滚动插件
 */
class ShiftHorizontalScrollPlugin extends ScrollbarPlugin {
  static pluginName = "shiftHorizontalScroll";

  transformDelta(delta: Data2d, fromEvent: Event) {
    if (!/wheel/.test(fromEvent.type) || !(fromEvent as WheelEvent).shiftKey) {
      return delta;
    }

    // @see: https://github.com/idiotWu/smooth-scrollbar/issues/181

    const { x, y } = delta;
    console.log(fromEvent);

    return {
      y: 0,
      x: Math.abs(x) > Math.abs(y) ? x : y,
      // x: Math.sign(x || y) * Math.sqrt(x*x + y*y),
    };
  }
}

Scrollbar.use(ShiftHorizontalScrollPlugin, OverscrollPlugin);
